/**
 * 
 */
package br.com.mateusfsilva.truco;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;

import android.util.Log;
import br.com.mateusfsilva.truco.card.Card;
import br.com.mateusfsilva.truco.card.Pack;

/**
 * @author Mateus
 *
 */
public class Hand
{
   private Card[][] cards = new Card[4][3];
   private Sprite[][] SpriteCards = new Sprite[4][3];
   private Pack pack;
   private Truco truco;
   private Scene scene;

   public Hand(Truco truco, Scene scene)
   {
      this.truco = truco;
      this.scene = scene;
      this.pack = Pack.getInstance();
      this.pack.Shuffle();

      NewHand();
   }

   public Card[][] getCards()
   {
      return this.cards;
   }

   public void NewHand()
   {
      //Player 01
      cards[0][0] = pack.GetCard();
      cards[0][1] = pack.GetCard();
      cards[0][2] = pack.GetCard();

      SpriteCards[0][0] = addCard(scene, cards[0][0], (int)(340 * truco.getRATIO_CAMERA_WIDTH()), (int)(010 * truco.getRATIO_CAMERA_HEIGHT()), 0, 0);
      SpriteCards[0][1] = addCard(scene, cards[0][1], (int)(360 * truco.getRATIO_CAMERA_WIDTH()), (int)(010 * truco.getRATIO_CAMERA_HEIGHT()), 0, 0);
      SpriteCards[0][2] = addCard(scene, cards[0][2], (int)(380 * truco.getRATIO_CAMERA_WIDTH()), (int)(010 * truco.getRATIO_CAMERA_HEIGHT()), 0, 0);

      //Player 02
      cards[1][0] = pack.GetCard();
      cards[1][1] = pack.GetCard();
      cards[1][2] = pack.GetCard();

      SpriteCards[1][0] = addCard(scene, cards[1][0], (int)(340 * truco.getRATIO_CAMERA_WIDTH()), (int)(374 * truco.getRATIO_CAMERA_HEIGHT()), 0, 1);
      SpriteCards[1][1] = addCard(scene, cards[1][1], (int)(360 * truco.getRATIO_CAMERA_WIDTH()), (int)(374 * truco.getRATIO_CAMERA_HEIGHT()), 0, 1);
      SpriteCards[1][2] = addCard(scene, cards[1][2], (int)(380 * truco.getRATIO_CAMERA_WIDTH()), (int)(374 * truco.getRATIO_CAMERA_HEIGHT()), 0, 1);

      //Player 03
      cards[2][0] = pack.GetCard();
      cards[2][1] = pack.GetCard();
      cards[2][2] = pack.GetCard();

      SpriteCards[2][0] = addCard(scene, cards[2][0], (int)(10 * truco.getRATIO_CAMERA_WIDTH()), (int)(172 * truco.getRATIO_CAMERA_HEIGHT()), 0, 2);
      SpriteCards[2][1] = addCard(scene, cards[2][1], (int)(10 * truco.getRATIO_CAMERA_WIDTH()), (int)(192 * truco.getRATIO_CAMERA_HEIGHT()), 0, 2);
      SpriteCards[2][2] = addCard(scene, cards[2][2], (int)(10 * truco.getRATIO_CAMERA_WIDTH()), (int)(212 * truco.getRATIO_CAMERA_HEIGHT()), 0, 2);

      //Player 04
      cards[3][0] = pack.GetCard();
      cards[3][1] = pack.GetCard();
      cards[3][2] = pack.GetCard();

      SpriteCards[3][0] = addCard(scene, cards[3][0], (int)(714 * truco.getRATIO_CAMERA_WIDTH()), (int)(172 * truco.getRATIO_CAMERA_HEIGHT()), 0, 3);
      SpriteCards[3][1] = addCard(scene, cards[3][1], (int)(714 * truco.getRATIO_CAMERA_WIDTH()), (int)(192 * truco.getRATIO_CAMERA_HEIGHT()), 0, 3);
      SpriteCards[3][2] = addCard(scene, cards[3][2], (int)(714 * truco.getRATIO_CAMERA_WIDTH()), (int)(212 * truco.getRATIO_CAMERA_HEIGHT()), 0, 3);
   }

   private Sprite addCard(final Scene pScene, final Card pCard, final int pX, final int pY, final int rotate, final int player)
   {
	   int width;
	   int height;
	   
	   if (truco.getRATIO_CAMERA_WIDTH() != 1.0 || truco.getRATIO_CAMERA_HEIGHT() != 1.0)
       {
          width = (int)(Card.CARD_WIDTH * truco.getRATIO_CAMERA_WIDTH());
          height = (int)(Card.CARD_HEIGHT * truco.getRATIO_CAMERA_HEIGHT());
       }
	   else
	   {
	      width = Card.CARD_WIDTH;
	      height = Card.CARD_HEIGHT;
	   }
	   
	   final Sprite sprite = new Sprite(pX, pY, width, height, this.truco.getmCardTotextureRegionMap().get(pCard))
      {
         boolean mGrabbed = false;
         int player;

        @Override
         public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
         {
            switch(pSceneTouchEvent.getAction())
            {
               case TouchEvent.ACTION_DOWN:
                  this.setScale(1.25f);
                  this.mGrabbed = true;

                  break;
               case TouchEvent.ACTION_MOVE:
                  if(this.mGrabbed)
                     {
                        this.setPosition(pSceneTouchEvent.getX() - Card.CARD_WIDTH / 2, pSceneTouchEvent.getY() - Card.CARD_HEIGHT / 2);
                     }

                  break;
               case TouchEvent.ACTION_UP:
                  if(this.mGrabbed)
                  {
                     this.mGrabbed = false;
                     this.setScale(1.0f);
                  }

                    //ThrowCard(this);
                  break;
            }
            return true;
         }
      };

      sprite.setRotation(rotate);

      pScene.getLastChild().attachChild(sprite);
      pScene.registerTouchArea(sprite);

      return sprite;
   }

   public void ThrowCard(Sprite sprite)
   {
     boolean r = this.scene.getLastChild().detachChild(sprite);
     if (r)
         Log.i("", "");
   }
}